home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * Aliases.c *
- * *
- * Line Printer Daemon using TCP/IP printer protocol *
- * *
- * -------------- The mail alias routines -------------- *
- * *
- * Written by Casper Boon, November, 1992. *
- * *
- * © 1992 Casper Boon. *
- * *
- ************************************************************************/
-
- #include "LPD.H"
- #include "CvtAddr.h"
- #include "lpdProtos.h"
-
- typedef struct
- {
- char who1[12];
- LongWord at1;
- char who2[12];
- LongWord at2;
- } aliasRec, *aliasPtr, **aliasHandle;
-
- static Handle aliases = NIL;
-
- char *hnw(char *str, char *who, char *host);
-
- /************************************************************************
- ************************************************************************/
- static char *hnw(char *str, char *who, char *host)
- {
- /* read who */
- while (*str && *str != '@' && *str != ' ' && *str != '\t')
- *who++ = *str++;
- *who = 0;
-
- switch (*str)
- {
- case '@': break; /* OK, now find where */
- case 0 :
- case ' ':
- case '\t': /* both bad, can't have user without host */
- log_printf("Bad entry : no host\n"); return NIL;
- break;
- }
- str++; /* skip the '@' */
-
- while (*str && *str != ' ' & *str != '\t')
- *host++ = *str++;
- *host = 0;
-
- while (*str && (*str == ' ' || *str == '\t') ) str++;
- return str;
- }
-
- /************************************************************************
- ************************************************************************/
- void ParseAlias(char *str)
- {
- char tbuf[128], *s;
- aliasRec alias;
- integer nAliases = 0;
-
- if ( !(str = hnw(str, alias.who1, tbuf)) ) return;
- if (ConvertStringToAddr(tbuf, &alias.at1)!=noErr)
- {
- log_printf("Bad Alias : cant resolve host\n");
- return;
- }
-
- if ( !(str = hnw(str, alias.who2, tbuf)) ) return;
- if (ConvertStringToAddr(tbuf, &alias.at2)!=noErr)
- {
- log_printf("Bad Alias : cant resolve host\n");
- return;
- }
-
- if (!aliases)
- aliases = NewHandle(sizeof(aliasRec));
- else
- {
- nAliases = ( GetHandleSize(aliases) / sizeof(aliasRec) );
- SetHandleSize( aliases, GetHandleSize(aliases) + sizeof(aliasRec) );
- }
- HLock(aliases);
- ((aliasRec*)(*aliases))[nAliases] = alias;
- HUnlock(aliases);
- if DEBUGGING
- {
- Byte *ip = (Byte*)&alias.at1, *id = (Byte*)&alias.at2;
- log_printf("ALIAS \"%s@%d.%d.%d.%d\" resolved \"%s@%d.%d.%d.%d\"\n",
- alias.who1, ip[0], ip[1], ip[2], ip[3],
- alias.who2, id[0], id[1], id[2], id[3]);
- }
- }
-
-
- /************************************************************************
- ************************************************************************/
- void ParseAdmin(char *str)
- {
- char tbuf[128], wbuf[20];
- LongWord hip;
- Byte *ip = (Byte*)&hip;
-
- if ( !(str = hnw(str, wbuf, tbuf)) ) return; /* no host */
- if (ConvertStringToAddr(tbuf, &hip)!=noErr)
- {
- log_printf("Bad Admin : cant resolve host\n");
- return;
- }
-
- strcpy(administrator, wbuf);
- strcpy(adminsHost, tbuf);
- gotAdmin = TRUE;
- if DEBUGGING
- log_printf("ADMIN \"%s@%s\" resolved \"%s@%d.%d.%d.%d\"\n",
- wbuf, tbuf, wbuf, ip[0], ip[1], ip[2], ip[3]);
- }
-
- /************************************************************************
- ************************************************************************/
- static void Addr2Str(char * buf, LongInt ip);
- static void Addr2Str(char * buf, LongInt ip)
- {
- Byte *th = (Byte*)&ip;
-
- if (ConvertAddrToString( buf, ip) != noErr)
- sprintf(buf, "%d.%d.%d.%d", th[0], th[1], th[2], th[3]);
- }
-
-
- /************************************************************************
- ************************************************************************/
- void ResolveAlias(char * who, char *host, char *who2, char *where)
- {
- LongWord ipAddress;
- integer nAliases, i;
- aliasPtr alias;
-
- strcpy(where, host);
- strcpy(who2, who);
-
- if (ConvertStringToAddr(host, &ipAddress)!=noErr)
- return;
-
- if (!aliases)
- return;
- else
- nAliases = (GetHandleSize(aliases) / sizeof(aliasRec));
-
- HLock(aliases); alias = (aliasPtr)(*aliases);
- for (i = 0; i < nAliases; i++, alias++)
- {
- if (alias->at1 == ipAddress)
- { /* matched the host */
- if ( (strlen(alias->who1) == 0) || (strcmp(alias->who1, who) == 0) )
- { /* all users or matched user */
- Addr2Str(where, alias->at2);
- if (strlen(alias->who2) != 0)
- strcpy(who2, alias->who2);
- break;
- }
- }
- }
- HUnlock(aliases);
- }
-